import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

def plot_results():
    # 读取结果数据
    data = pd.read_csv("output.csv", header=None)
    # 设置列名方便引用
    data.columns = ['a', 'b', 'epsilon', 'result', 'n', 'step_size', 'algorithm']

    # 分离两种算法的数据
    trapezoidal_data = data[data['algorithm'] == '复化梯形公式'].copy()
    romberg_data = data[data['algorithm'] == '龙贝格算法'].copy()

    # 计算 epsilon 的对数值
    trapezoidal_data['log_epsilon'] = np.log10(trapezoidal_data['epsilon'])  
    romberg_data['log_epsilon'] = np.log10(romberg_data['epsilon'])
    # 计算 epsilon 的对数值
    trapezoidal_data['log_n'] = np.log2(trapezoidal_data['n'])  
    romberg_data['log_n'] = np.log2(romberg_data['n'])


    # 绘图
    fig, axes = plt.subplots(1, 2, figsize=(14, 6))
    
    # 复化梯形公式
    axes[0].scatter(trapezoidal_data['log_epsilon'], trapezoidal_data['log_n'], color='blue', label='Trapezoidal Rule', marker='o')
    # axes[0].scatter(trapezoidal_data['log_epsilon'], trapezoidal_data['result'], color='blue', label='Trapezoidal Rule', marker='o')
    axes[0].set_title('n of the Trapezoidal Rule')
    axes[0].set_xlabel('Number of Subdivisions (n)')
    axes[0].set_ylabel('Integration Result')
    axes[0].grid(True)
    axes[0].legend()

    # 龙贝格算法
    axes[1].scatter(romberg_data['log_epsilon'], romberg_data['log_n'], color='orange', label='Romberg Method', marker='s')
    # axes[1].scatter(romberg_data['log_epsilon'], romberg_data['result'], color='orange', label='Romberg Method', marker='s')
    axes[1].set_title('n of the Romberg Method')
    axes[1].set_xlabel('Number of Subdivisions (n)')
    axes[1].set_ylabel('Integration Result')
    axes[1].grid(True)
    axes[1].legend()

    # 调整图形
    plt.tight_layout()
    plt.show()

if __name__ == "__main__":
    plot_results()
